<?php

class Gmap extends Controller {

	function Gmap()
	{
		parent::Controller();
		$this->load->model('gmap/gmap_model');
	}
	
	function index()
	{
		$this->load->helper('url');
		$this->load->view('gmap/index');
	}
	
	function saveLatLong() {
		$data['latitude'] = $this->input->post('lat');
		$data['longitude']= $this->input->post('lng');
		$data['shelter_name'] = $this->input->post('name');
		echo $this->gmap_model->Insert('shelter', $data);
	}
	
	function loadMarkers() {
		$markers = $this->gmap_model->Load('shelter');
		
		$strMarkers = '<markers>';
		foreach ($markers as $marker) {
			$strMarkers .= "<marker id='{$marker->id_shelter}' name='{$marker->shelter_name}' lat='{$marker->latitude}' lng='{$marker->longitude}' />";
		}
		$strMarkers .= '</markers>';
		
		header("Content-type: text/xml"); 
		echo $strMarkers;
	}
	
	function loadRoutes() {
		$routes = $this->gmap_model->Load('route');
		echo json_encode($routes);
	}
	
	function saveMarker() {
		$data['latitude'] 	= $this->input->post('lat');
		$data['longitude'] 	= $this->input->post('lng');
		$id = $this->input->post('id');
		
		$this->gmap_model->update(array('id_shelter'=>$id), $data);
	}
	
	function saveRouteDetail() {
		$data['id_route'] 		= $this->input->post('id_route');
		$data['id_shelter_from']= $this->input->post('id_from');
		$data['id_shelter_to'] 	= $this->input->post('id_to');
				
		echo $this->gmap_model->Insert('route_detail', $data);
	}
	
	function saveRouteDetailVertex() {
		$id_route_detail	= $this->input->post('id_route_detail');
		$arr_latlng			= json_decode($this->input->post('latlng'));
		
		foreach ($arr_latlng as $latlng) {
			$data['id_route_detail']= $id_route_detail;
			$data['lat_start']		= $latlng[0];
			$data['lng_start']		= $latlng[1];
			$data['lat_end']		= $latlng[2];
			$data['lng_end']		= $latlng[3];
			
			$this->gmap_model->Insert('route_detail_vertex', $data);
		}
		print_r($arr_latlng);
	}
}

?>